﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HexEditor_
{

    public class DataTypeChar : DataType
    {
        string[] ASCII_UNDER32 = {
            "NUL",
            "SOH",
            "STX",
            "ETX",
            "EOT",
            "ENQ",
            "ACK",
            "BEL",
            "BS",
            "HT",
            "LF",
            "VT",
            "FF",
            "CR",
            "SO",
            "SI",
            "DLE",
            "DC1",
            "DC2",
            "DC3",
            "DC4",
            "NAK",
            "SYN",
            "ETB",
            "CAN",
            "EM",
            "SUB",
            "ESC",
            "FS",
            "GS",
            "RS",
            "US",
            "(space)"
        };
        public string ByteToString(byte b)
        {
            if (b < ASCII_UNDER32.Length)
                return ASCII_UNDER32[b];
            if (b == 127)
                return "DEL";
            return Convert.ToChar(b).ToString();
        }
        public string GetName() { return "char"; }
        public int GetBytesRequired() { return 1; }

        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = ByteToString(bytes[offset]);
            return GetBytesRequired();
        }

        public string GetExample() { return "Hello World"; }
    }

    public class DataTypeInt : DataType
    {
        public string GetName() { return "int"; }
        public int GetBytesRequired() { return 4; }

        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToInt32(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "-123456789"; }
    }

    public class DataTypeFloat : DataType
    {
        public string GetName() { return "float"; }
        public int GetBytesRequired() { return 4; }

        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToSingle(bytes, offset).ToString();
            return GetBytesRequired();
        }

        public string GetExample() { return "1.988774"; }
    }

    public class DataTypeDouble : DataType
    {
        public string GetName() { return "double"; }
        public int GetBytesRequired() { return 8; }

        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToDouble(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "199.998376758"; }
    }
    public class DataTypeShort : DataType
    {
        public string GetName() { return "short"; }
        public int GetBytesRequired() { return 2; }
        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToInt16(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "-12345"; }
    }

    public class DataTypeUshort : DataType
    {
        public string GetName() { return "ushort"; }
        public int GetBytesRequired() { return 2; }
        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToUInt16(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "12345"; }
    }

    public class DataTypeUint : DataType
    {
        public string GetName() { return "uint"; }
        public int GetBytesRequired() { return 4; }
        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToUInt32(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "1245546466"; }
    }

    public class DataTypeInt64 : DataType
    {
        public string GetName() { return "int64"; }
        public int GetBytesRequired() { return 8; }
        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToInt64(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "-1231434543667777"; }
    }

    public class DataTypeUint64 : DataType
    {
        public string GetName() { return "uint64"; }
        public int GetBytesRequired() { return 8; }
        public int Parse(byte[] bytes, int offset, out string res)
        {
            res = BitConverter.ToUInt64(bytes, offset).ToString();
            return GetBytesRequired();
        }
        public string GetExample() { return "1231434543667777"; }
    }
}
